<template>
  <div class="business-orderchange-container">
    <!-- <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item label="异动查询">
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form> -->

    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="异动监控" name="all">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="异动id"
            prop="OrderChangeID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="异动时间"
            prop="addTime"
            show-overflow-tooltip
            width="180"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="300"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="异动产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="产品类型"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <div v-if="row.ProductType == 0">产品</div>
              <div v-else>赠品</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="异动类型"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <div v-if="row.ActType == 1">换店长组</div>
              <div v-if="row.ActType == 2">换运营师</div>
              <div v-if="row.ActType == 3">店铺暂停</div>
              <!-- <div v-if="row.ActType == 4">店铺恢复运营</div> -->
              <div v-if="row.ActType == 5">不再续费</div>
              <div v-if="row.ActType == 6">退款</div>
              <div v-if="row.ActType == 7">服务终止</div>
              <div v-if="row.ActType == 8">换店服务</div>
              <div v-if="row.ActType == 9">延期服务</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="操作人"
            prop="ActStaffName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="异动说明"
            prop="ChangeMemo"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="handleEdit(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="服务暂停中待恢复" name="paused">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="异动产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="暂停周期" width="250">
            <template #default="{ row }">
              <div v-for="(item, index) in row.PausedDates" :key="index">
                <span style="color: tomato">
                  {{ item.StartDate }}至{{ item.EndDate }}
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="服务周期" width="250">
            <template #default="{ row }">
              <div v-for="(item, index) in row.ServiceDates" :key="index">
                <span style="color: deepskyblue">
                  {{ item.StartDate }}至{{ item.EndDate }}
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(row, 31)"
              >
                恢复服务
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="服务中待暂停" name="needpause">
        <el-table
          v-loading="listLoading"
          :data="list"
          @selection-change="setSelectRows"
        >
          <el-table-column
            align="center"
            show-overflow-tooltip
            type="selection"
          />
          <el-table-column
            align="center"
            label="合同ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column
            align="center"
            label="客户"
            prop="CustomerAllName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="合同编号"
            prop="OrderCode"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="异动产品"
            prop="ProductName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="暂停周期" width="250">
            <template #default="{ row }">
              <div v-for="(item, index) in row.PausedDates" :key="index">
                <span style="color: tomato">
                  {{ item.StartDate }}至{{ item.EndDate }}
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="服务周期" width="250">
            <template #default="{ row }">
              <div v-for="(item, index) in row.ServiceDates" :key="index">
                <span style="color: deepskyblue">
                  {{ item.StartDate }}至{{ item.EndDate }}
                </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="200">
            <template #default="{ row }">
              <el-button
                type="primary"
                size="small"
                @click="handleEdit(row, 4)"
              >
                操作暂停
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getOrderchangeList, doDelete } from '@/api/businessOrderchange'
  import Edit from './components/BusinessOrderchangeEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'BusinessOrderchange',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          tab: 'all',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row, type) => {
        // const form = Object.assign({}, row)
        row.ServiceState = type
        console.log('handleEdit', row)
        state.editRef.showEdit(row)
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }

      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getOrderchangeList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleDelete,
        handleTabClick,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
